#include "HitFx.h"
#include "global.h"

HitFx::HitFx (float x,float y)
{
	 CCArray *animFrames=new CCArray();

	CCSpriteFrame* pFrame0 = CCSpriteFrameCache::sharedSpriteFrameCache()
		->spriteFrameByName("hit_0_0.png");
	animFrames->addObject(pFrame0);
	CCSpriteFrame* pFrame1 =CCSpriteFrameCache::sharedSpriteFrameCache()
		->spriteFrameByName("hit_0_1.png");
	animFrames->addObject(pFrame1);
	CCSpriteFrame* pFrame2 = CCSpriteFrameCache::sharedSpriteFrameCache()
		->spriteFrameByName("hit_0_2.png");
	animFrames->addObject(pFrame2);

	CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames,0.1f);
	
	
	this->initWithSpriteFrame((CCSpriteFrame *)animFrames->objectAtIndex(0));animFrames->release();
	this->setPosition(ccp(x, y));
	this->runAction(CCSequence::create(
		CCAnimate::create(animation),
		CCCallFunc::create(this, callfunc_selector(HitFx::destroy)),NULL));
}

void HitFx::destroy()
{
	this->getParent()->removeChild(this, true);
}
BombFx::BombFx (float x,float y,int vscore)
{
		 CCArray *animFrames=new CCArray();

	CCSpriteFrame* pFrame0 = CCSpriteFrameCache::sharedSpriteFrameCache()
		->spriteFrameByName("bomb.png");
	animFrames->addObject(pFrame0);
	CCSpriteFrame* pFrame1 =CCSpriteFrameCache::sharedSpriteFrameCache()
		->spriteFrameByName("bomb1.png");
	animFrames->addObject(pFrame1);
	CCSpriteFrame* pFrame2 = CCSpriteFrameCache::sharedSpriteFrameCache()
		->spriteFrameByName("bomb2.png");
	animFrames->addObject(pFrame2);

	CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames,0.1f);
	
	
	this->initWithSpriteFrame((CCSpriteFrame *)animFrames->objectAtIndex(0));animFrames->release();
	this->setPosition(ccp(x, y));
	this->runAction(CCSequence::create(
		CCAnimate::create(animation),
		CCCallFunc::create(this, callfunc_selector(BombFx::destroy)),NULL));

	isme=false;
	score=vscore;
	
}

void BombFx::destroy()
{

	//if (global_isBomb)
	{
		if (isme)
		{
		}
		else
		{
			CCPoint the=this->getPosition();
			//ma BonusScore bon= new BonusScore(score,(int) the.x, (int)the.y);
			//ma global_instance.grass.addChild(bon);
			BonusScore *bons = new BonusScore(score, the.x,the.y);bons->autorelease();
			global_instance->grass->addChild(bons);
			switch (score)
			{
			case 100:
				map_levelScore[0]+=1;
				break;
			case 200:
				map_levelScore[1]+=1;
				break;
			case 300:
				map_levelScore[2]+=1;
				break;
			}

		}					//trace(map_levelScore)
	}
	this->getParent()->removeChild(this, true);
}

Bonus::Bonus ()
{



	//ma Random r=new Random();
	//r.nextFloat();
	int xx = (float)rand()/(RAND_MAX+1.0)*26;
	int yy =(float)rand()/(RAND_MAX+1.0)*26;


	this->setPosition(ccp(xx*8, yy*8));


	int i=(int)((float)rand()/(RAND_MAX+1.0)* 5);
	//int i=0;
	//the.gotoAndStop(i);
	//this->initWithSpriteFrame((CCSpriteFrame *)animFrames->objectAtIndex(i));
	this->type = i;
	switch(i)
	{
	case 0:
		this->initWithSpriteFrameName("bao0.jpg");break;
	case 1:
		this->initWithSpriteFrameName("bao1.jpg");break;
	case 2:
		this->initWithSpriteFrameName("bao2.jpg");break;
	case 3:
		this->initWithSpriteFrameName("bao3.jpg");break;
	case 4:
		this->initWithSpriteFrameName("bao4.jpg");break;
	case 5:
		this->initWithSpriteFrameName("bao5.jpg");break;
	default:
		this->initWithSpriteFrameName("bao0.jpg");break;
	}

	this->runAction(CCSequence::create(
		CCDelayTime::create(16.0f),
		CCCallFunc::create(this, callfunc_selector(Bonus::destroy)),NULL));

}

void Bonus::destroy()
{
	this->getParent()->removeChild(this, true);
	//ma var bon:MyBonusScore = new MyBonusScore(new BonusScore(), 500, the.x + 15, the.y + 14);
	//MyMain.instance.grass.add(bon.the);
}

BonusScore::BonusScore (int score,float x,float y)
{
	int i=score/100-1;
	switch(i)
	{
	case 0:
		this->initWithSpriteFrameName("score_0_0.png");break;
	case 1:
		this->initWithSpriteFrameName("score_0_1.png");break;
	case 2:
		this->initWithSpriteFrameName("score_0_2.png");break;
	case 3:
		this->initWithSpriteFrameName("score_0_3.png");break;
	case 4:
		this->initWithSpriteFrameName("score_0_4.png");break;
	default:
		this->initWithSpriteFrameName("score_0_0.png");break;
	}
	this->setScale(1.5);
	this->setPosition(ccp(x,y));
	//this->initWithSpriteFrame((CCSpriteFrame *)animFrames->objectAtIndex(score/100-1));
	//animFrames->release();
	this->runAction(CCSequence::create(
		CCDelayTime::create(2.0f),
		CCCallFunc::create(this, callfunc_selector(Bonus::destroy)),NULL));


}

void BonusScore::destroy()
{
	this->getParent()->removeChild(this, true);
	//ma var bon:MyBonusScore = new MyBonusScore(new BonusScore(), 500, the.x + 15, the.y + 14);
	//MyMain.instance.grass.add(bon.the);
}
GameOver::GameOver (float x,float y)
{
	this->initWithSpriteFrameName("gameover.png");
	this->setPosition(ccp(x,y+this->getContentSize().height/2));
	CCMoveTo *a1=CCMoveTo::create(2.0,ccp(x,y+208));
	this->runAction(CCSequence::create(
		a1,
		CCCallFunc::create(this, callfunc_selector(GameOver::destroy)),NULL));
}
void GameOver::destroy()
{
	this->getParent()->removeChild(this, true);
	//global_overout = true;
	//MyMain.setBoard();
	//the.kill();
	//Tools.instance.remove(the);

	global_instance->replay();
}
BonusA::BonusA (float x,float y)
{
    
	this->setPosition(ccp(x,y));
    
    
    CCArray *animFrames=new CCArray();
    
	CCSpriteFrame* pFrame0 = CCSpriteFrameCache::sharedSpriteFrameCache()
    ->spriteFrameByName("bao0.jpg");
	animFrames->addObject(pFrame0);
	CCSpriteFrame* pFrame1 =CCSpriteFrameCache::sharedSpriteFrameCache()
    ->spriteFrameByName("bao1.jpg");
	animFrames->addObject(pFrame1);
	CCSpriteFrame* pFrame2 = CCSpriteFrameCache::sharedSpriteFrameCache()
    ->spriteFrameByName("bao2.jpg");
	animFrames->addObject(pFrame2);
    CCSpriteFrame* pFrame3 = CCSpriteFrameCache::sharedSpriteFrameCache()
    ->spriteFrameByName("bao3.jpg");
	animFrames->addObject(pFrame3);
    CCSpriteFrame* pFrame4 = CCSpriteFrameCache::sharedSpriteFrameCache()
    ->spriteFrameByName("bao4.jpg");
	animFrames->addObject(pFrame4);
    CCSpriteFrame* pFrame5 = CCSpriteFrameCache::sharedSpriteFrameCache()
    ->spriteFrameByName("bao5.jpg");
	animFrames->addObject(pFrame5);
    
	CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames,0.5f);
	
    cocos2d::CCRepeatForever * r=CCRepeatForever::create(CCAnimate::create(animation));
	this->initWithSpriteFrame((CCSpriteFrame *)animFrames->objectAtIndex(0));animFrames->release();
	this->setPosition(ccp(x, y));
	this->runAction(r);
}
